package com.source.module_ipc_server.activity;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;

import com.source.module_ipc_client.IPCClientMainActivity;
import com.source.module_ipc_server.IProcessInfo;
import com.source.module_ipc_server.R;
import com.source.module_ipc_server.base.Constant;
import com.source.module_ipc_server.service.IPCRemoteService;
import com.source.module_ipc_server.service.LocalService;

/**
 * @author Created by wsl
 * @des
 * @date 2022/6/9
 */
public class IPCMainActivity extends AppCompatActivity implements View.OnClickListener {
    private final String Tag = this.getClass().getSimpleName() + "_";
    private LocalService.MyBinder binder;

    public static void startActivity(Context context) {
        Intent intent = new Intent(context, IPCMainActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ipc_main);
        findViewById(R.id.btn_start).setOnClickListener(this);
        findViewById(R.id.btn_stop).setOnClickListener(this);
        findViewById(R.id.btn_bind).setOnClickListener(this);
        findViewById(R.id.btn_unbind).setOnClickListener(this);
        findViewById(R.id.btn_start_download).setOnClickListener(this);
        findViewById(R.id.btn_stop_download).setOnClickListener(this);
        findViewById(R.id.btn_downloading).setOnClickListener(this);
        findViewById(R.id.btn_complete_download).setOnClickListener(this);
        findViewById(R.id.btn_bind_remote).setOnClickListener(this);
        findViewById(R.id.btn_unbind_remote).setOnClickListener(this);
        findViewById(R.id.btn_start_remote_activity).setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.btn_start) {
            Log.e(Constant.TAG, Tag + "btn_start");
            Intent startIntent = new Intent(IPCMainActivity.this, LocalService.class);
            startService(startIntent);
        } else if (id == R.id.btn_stop) {
            Log.e(Constant.TAG, Tag + "btn_stop");
            Intent stopIntent = new Intent(IPCMainActivity.this, LocalService.class);
            stopService(stopIntent);
        } else if (id == R.id.btn_bind) {
            Log.e(Constant.TAG, Tag + "btn_bind");
            Intent bindIntent = new Intent(IPCMainActivity.this, LocalService.class);
            bindService(bindIntent, myConnection, BIND_AUTO_CREATE);
        } else if (id == R.id.btn_unbind) {
            Log.e(Constant.TAG, Tag + "btn_unbind");
            unbindService(myConnection);
        } else if (id == R.id.btn_start_download) {
            Log.e(Constant.TAG, Tag + "btn_start_download");
            if (binder != null) {
                binder.startDownload();
            }
        } else if (id == R.id.btn_downloading) {
            Log.e(Constant.TAG, Tag + "btn_downloading");
            if (binder != null) {
                binder.downloading();
            }
        } else if (id == R.id.btn_complete_download) {
            Log.e(Constant.TAG, Tag + "btn_complete_download");
            if (binder != null) {
                binder.completeDownload();
            }
        } else if (id == R.id.btn_stop_download) {
            Log.e(Constant.TAG, Tag + "btn_stop_download");
            if (binder != null) {
                binder.stopDownload();
            }
        } else if (id == R.id.btn_bind_remote) {
            Log.e(Constant.TAG, Tag + "btn_bind_remote");
            Intent bindIntent = new Intent(IPCMainActivity.this, IPCRemoteService.class);
            bindService(bindIntent, remoteConnection, BIND_AUTO_CREATE);
        } else if (id == R.id.btn_unbind_remote) {
            Log.e(Constant.TAG, Tag + "btn_unbind_remote");
            unbindService(remoteConnection);
        } else if (id == R.id.btn_start_remote_activity) {
            Log.e(Constant.TAG, Tag + "btn_unbind_remote");
            Intent bindIntent = new Intent(IPCMainActivity.this, IPCClientMainActivity.class);
            startActivity(bindIntent);
        }
    }

    private ServiceConnection myConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            Log.e(Constant.TAG, Tag + "onServiceConnected");
            binder = (LocalService.MyBinder) service;
            binder.addCallBack(new LocalService.MyBinder.MyCallback() {
                @Override
                public void call(int num) {
                    Log.e(Constant.TAG, Tag + "MyCallback.call.num=" + num);
                }
            });
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            Log.e(Constant.TAG, Tag + "onServiceDisconnected");
        }
    };
    private ServiceConnection remoteConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            Log.e(Constant.TAG, Tag + "onServiceConnected");
            IProcessInfo iProcessInfo = IProcessInfo.Stub.asInterface(service);
            try {
                Log.e(Constant.TAG, Tag + "onServiceConnected_ProcessId" + iProcessInfo.getProcessId());
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            Log.e(Constant.TAG, Tag + "onServiceDisconnected");
        }
    };

}
